package org.summer.lawyer.pay.client.impl.wechat;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import lombok.extern.slf4j.Slf4j;
import org.summer.lawyer.domain.bo.payorder.PayOrderBo;
import org.summer.lawyer.domain.vo.paychannel.PayChannelVo;
import org.summer.lawyer.domain.vo.payorder.PayOrderVo;

/**
 *
 */
@Slf4j
public class WeChatMiniAppPayClient extends AbstractWeChatPayClient{

//    public WeChatMiniAppPayClient(Long channelId, WeChatPayClientConfig config){
//        super(channelId, PaymentMethod.WECHAT_MINIAPP.getCode(), config);
//    }

    public WeChatMiniAppPayClient(PayChannelVo vo){
        super(vo);
    }

    @Override
    protected PayOrderVo doUnifiedPayV2(PayOrderBo bo) throws WxPayException {
        // 构建 WxPayUnifiedOrderRequest 对象
        WxPayUnifiedOrderRequest request = buildUnifiedPayRequestV2(bo)
                .setOpenid(bo.getOpenId());
        // 执行请求
        WxPayMpOrderResult response = wxPayService.createOrder(request);
        //转换结果
        return PayOrderVo.pendingOf(bo.getPayNo(), response);
    }

    @Override
    protected PayOrderVo doUnifiedPayV3(PayOrderBo bo) throws WxPayException {
// 构建 WxPayUnifiedOrderRequest 对象
        WxPayUnifiedOrderV3Request request = buildUnifiedPayRequestV3(bo)
                .setPayer(new WxPayUnifiedOrderV3Request.Payer().setOpenid(bo.getOpenId()));
        // 执行请求
        WxPayUnifiedOrderV3Result.JsapiResult response = wxPayService.createOrderV3(TradeTypeEnum.JSAPI, request);

        // 转换结果
        return PayOrderVo.pendingOf(bo.getPayNo(), response);
    }

    @Override
    protected void doInit() {
        super.doInit(WxPayConstants.TradeType.JSAPI);
    }
}
